/**
 * 
 */
package py.com.alfa.escriba.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dto.ControlesInfoDTO;

public class Utils {
  public static boolean isWhitespace(String str) {
    if (str == null) {
      return false;
    }
    int sz = str.length();
    for (int i = 0; i < sz; i++) {
      if ((Character.isWhitespace(str.charAt(i)) == false)) {
        return false;
      }
    }
    return true;
  }

  public static void validarDTO(Object dto) throws ServiceException{
    Validator validator = new Validator();
    List<ConstraintViolation> violations = validator.validate(dto);
    
    if(violations.size()>0)
    {
    	String errors = printConstraintViolation(violations);
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"param.validation.error",errors);
    }
  }
  
  public static void validarDTO(Object dto, String codigo) throws ServiceException{
    validarDTO(dto);
    
    try {
      Long.parseLong(codigo.substring(0,1));
      throw new ServiceException(ErrorCode.PARAMETER_ERROR, "error.param.codigo.number", codigo);
    }
    catch (NumberFormatException e){/*no empieza con número, esta bien*/}
    
  }
  
  private static String printConstraintViolation(List<ConstraintViolation> violations){
	  StringBuffer sb = new StringBuffer();
	  for (Iterator<ConstraintViolation> it = violations.iterator(); it.hasNext();) {
		ConstraintViolation violation = (ConstraintViolation) it.next();
		sb.append("- ").append(violation.getMessage());
		if(it.hasNext()) sb.append("\n");
	  }
	  return sb.toString();
  }
  
  public static void validarAlfaNumerico(String valor) throws ServiceException{
	    
	    try {
	     for(int i=0;i<valor.length();i++){
	    	 char caracter =valor.charAt(i);
	    	 if (!Character.isLetterOrDigit(caracter)){
	    		 throw new ServiceException(ErrorCode.PARAMETER_ERROR, "error.param.not.alfanumerico", caracter);
	    	 }
	     }
	      
	    }
	    catch (NumberFormatException e){/*no empieza con número, esta bien*/}
	    
  }
  
  public static void validarControl(ControlesInfoDTO control) throws ServiceException{

   	 if (control.getPersonas()==null || control.getPersonas().size()==0){
	     throw new ServiceException(ErrorCode.PARAMETER_ERROR, "controles.not.persona");
   	 }
     if (control.getNaturalezas()==null || control.getNaturalezas().size()==0){
		 throw new ServiceException(ErrorCode.PARAMETER_ERROR, "controles.not.naturaleza");
   	 }
  }
  
  public static Date calcularVencimiento(Date fecha, Integer dias){
	  Calendar calendar = Calendar.getInstance();
	  calendar.setTime(fecha); // Configuramos la fecha que se recibe
	  calendar.add(Calendar.DAY_OF_YEAR, dias);  // numero de d�as a a�adir, o restar en caso de d�as<0
	  return calendar.getTime();  
  }
	  
}
